#!/bin/sh
#desc:Local storage

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

mkdir -p /media/

# Por precaución, reaplicamos permisos importantes
chown admin:admin /media
chown admin:admin /mnt/
chown admin:admin -R /usr/share/
ls -lah /var > /tmp/varlist.txt

/usr/bin/logger "Mounting drives"


# En el arranque predeterminado solamente funcionan las primeras particiones
DRIVEORDER="sr0 sda sda1 sdb sdb1 sdc sdc1 hda hda1 hdb hdb1 hdc hdc1 hdd hdd1"

# Parametros: [ruta donde se ha montado] 
process_config(){
	device="$1"
	MDCFG="/mnt/$1/opendomo.cfg"

	#### Lectura desconfiada de los archivos de configuración 
	CONFDEVICE=`grep ^CONFDEVICE= $MDCFG | sed 's/\"//g' | cut -f2 -d= `
	SYSDEVICE=`grep ^SYSDEVICE= $MDCFG | sed 's/\"//g' | cut -f2 -d= `
	SYSBACK=`grep ^SYSBACK= $MDCFG | sed 's/\"//g' | cut -f2 -d= `
	LABEL=`grep ^LABEL= $MDCFG | sed 's/\"//g' | cut -f2 -d=`
	LOGDIR=`grep ^LOGDIR= $MDCFG | sed 's/\"//g' | cut -f2 -d=`
	####
	
	echo " Device $device has this info: $CONFDEVICE - $SYSDEVICE - $SYSBACK - $LABEL - $LOGDIR" > /tmp/DEBUGMOUNT
	if [ "$CONFDEVICE" == "1" ]; then
		logger "  Device $device is Configuration drive"
		# Sobreescribimos el anterior (puede ser la ISO montada)
		test -d /mnt/odconf && rm /mnt/odconf 2>/dev/null
		# Do not link read-only filesystems
		if touch /mnt/$device/opendomo.cfg 2>/dev/null
		then
			logger "  Device $device linked"
			/bin/ln -s /mnt/$device /mnt/odconf  2>/dev/null
     	fi
     	 
		if test -d /mnt/$device/pkgcache/; then
			logger "  Device $device has pkgcache"
			for p in /mnt/$device/pkgcache/* ; do
				pkg_add $p >/dev/null 2>/dev/null
			done
		fi
		
		config="/mnt/$device/sysconf/sysconf.tar.gz"
		if test -f $config; then
			logger "  Loading configuration file $config "
			/usr/local/opendomo/services/syscript/loadSystemConfiguration.sh $config >/dev/null
		fi
	fi
	
	if [ "$SYSDEVICE" == "1" ]; then
		logger "  Device $device linked as System drive"
		/bin/ln -s /mnt/$device /mnt/system  2>/dev/null
		mkdir -p /mnt/system/home/ && chown admin /mnt/system/home/
		mkdir -p /mnt/system/libs/ && chown admin /mnt/system/libs/
		if test -f /mnt/system/VERSION; then
			cp /mnt/system/VERSION /etc/
		fi
		
		if test -d /mnt/system/home; then
			rm -fr /home
			ln -s /mnt/system/home/ /home
		fi
	fi

	if [ "$SYSBACK" == "1" ]; then
		logger "  Device $device linked as System backup"
		/bin/ln -s /mnt/$device /mnt/sysback
	fi

	if [ "$LOGDIR" != "" ]; then
		if test -d /mnt/$device/$LOGDIR; then
			logger "$CONF_LOG is the logs dir"
			mv /var/log/ /var/slog/
			/bin/ln -s /mnt/$device/$LOGDIR /var/log 2>/dev/null
		fi
	fi
	
	if [ "$LABEL" != "" ]; then
		logger "  Device $device is also known as '$LABEL' "
		/bin/ln -s /mnt/$device /media/$LABEL 2>/dev/null
	fi
}


start(){
  # Hack for ISO auto-mounting and misconfigured systems
  OPTIONS="rw,nosuid,uid=1000,gid=100,dmask=0027,fmask=0027,flush"
  for d in sr0 sda sdb sdc hda hdb hdc hdd; do
	  if test -e /dev/$d && ! test -e /mnt/$d; then
	  	mkdir /mnt/$d
	  	# Try to mount with options, otherwise without.
	  	mount /dev/$d /mnt/$d -o $OPTIONS || mount /dev/$d /mnt/$d 
	  fi
  done

  
  FSORDER="ext3 ext2 iso9660 vfat"
  for dev in $DRIVEORDER; do
	#for fs in $FSORDER; do
		#if ! test -d /mnt/$dev; then
		#	# Tener en cuenta los tipos particulares de FS
		#	if test "$fs" = "vfat"; then
		#		# Posiblemente se trate de un pendrive, ya que
		#		# no es ningún sistema nativo de Linux. Hay que
		#		# dar acceso a todos los usuarios, ya que el 
		#		# owner será root:root.
		#		op=" -o fmask=0111,dmask=0000,sync "
		#	else
		#		op=""
		#	fi
		#	mkdir -p /mnt/$dev
		#	if mount -t $fs /dev/$dev /mnt/$dev $op 2>/dev/null
			if test -d /mnt/$dev
			then
				/usr/bin/logger "Mounting $dev"
				echo -n "($dev)"
				logger "Device $dev detected"
				if test -e /mnt/$dev/opendomo.cfg; then
					process_config $dev
				else
					# Creamos el archivo de configuración y le asignamos permisos
					# para que pueda configurarse desde la interfaz
					touch /mnt/$dev/opendomo.cfg 2>/dev/null
					chown admin /mnt/$dev/opendomo.cfg 2>/dev/null
				fi
#				break;
			else 
				/usr/bin/logger "$dev not found"
		#		rm -fr /mnt/$dev
			fi
		#fi
	#done
  done
}


stop(){
	sync; sync
	for i in /mnt/*; do
		umount /dev/$i 2> /dev/null
		echo -n "($i)"
	done
	/bin/umount -a -r 2> /dev/null
	/sbin/swapoff -a 2> /dev/null
}

case $1 in 
  start)
	echo -n "Mounting drives..."
	start
  ;;
  stop)
	echo -n "Unmounting filesystems..."
	stop
  ;;
  reload|restart)
	echo -n "Re-checking drives..."
	start
  ;;
  *)
	echo "Usage: $0 {start|stop|restart|reload}"
	exit 1

esac
exit 0
